// 引入SDK核心类
// var QQMapWX = require('../../utils/qqmap-wx-jssdk');

var mks = [];
mks.push({ // 获取返回结果，放到mks数组 中

  id: 1,
  latitude: 39.942845,
  longitude: 116.481793,
  iconPath: 'https://www.juquduo.com/static/resourse/yeshou/火苗-3.png',
  placename:'朝阳公园',
  width: 40,
  height: 40,
},{ // 获取返回结果，放到mks数组中
  id: 2,
  latitude: 39.958786,
  longitude: 116.46713,
  iconPath: 'https://www.juquduo.com/static/resourse/yeshou/火苗-2.png',
  placename:'天泽路',
  width: 40,
  height: 40,
},{ // 获取返回结果，放到mks数组中
  id: 3,
  latitude: 40.411951,
  longitude: 116.314284,
  iconPath: 'https://www.juquduo.com/static/resourse/yeshou/火苗-2.png',
  placename:'水长城',
  width: 40,
  height: 40,
},{ // 获取返回结果，放到mks数组中
  id: 4,
  latitude: 40.648427,
  longitude: 117.273023,
  iconPath: 'https://www.juquduo.com/static/resourse/yeshou/火苗-2.png',
  placename:'古北水镇',
  width: 40,
  height: 40,
},{ // 获取返回结果，放到mks数组中
  id: 5,
  latitude: 40.948302,
  longitude: 115.437370,
  iconPath: 'https://www.juquduo.com/static/resourse/yeshou/火苗-2.png',
  placename:'云顶酒店',
  width: 40,
  height: 40,
},{ // 获取返回结果，放到mks数组中
  id: 6,
  latitude: 39.881287,
  longitude: 116.410654,
  iconPath: 'https://www.juquduo.com/static/resourse/yeshou/火苗-2.png',
  placename:'天坛公园',
  width: 40,
  height: 40,
},{ // 获取返回结果，放到mks数组中
  id: 7,
  latitude: 39.812208,
  longitude: 116.489191,
  iconPath: 'https://www.juquduo.com/static/resourse/yeshou/火苗-1.png',
  placename:'鸿博公园',
  width: 40,
  height: 40,
});
var poi= {
  latitude: 40.648427,
  longitude: 117.273023,
}
 
var app = getApp()
// 实例化API核心类
// var qqmapsdk = new QQMapWX({
//     key: app.globalData.key // 必填
// });  
 
//在Page({})中使用下列代码
Page({
  data:{
    winheight:app.globalData.winheight,
    winwidth:app.globalData.winwidth,
    widd:app.globalData.winwidth/2-40,
    userInfo: app.globalData.userInfo,
    heii:app.globalData.winwidth/10,
    poiname:'',
    shuru:'',
    mks:[],markers:[],
    imagepath:'https://www.juquduo.com/static/resourse/yeshou/头像-1.png',
    poi: poi
  },
  onShow(){
    this.setData({poi: poi})
  },
  send :function (res){ //输入框按下回车
    var that = this
    //获得文本框输入的值
    var name = res.detail.value;
    var haspoint = 1
    this.setData({poiname:name})
    console.log(this.data.poiname)
    for(var i = 0;i<mks.length;i++){
      if(name == mks[i].placename){
        that.setData({poi: {
          latitude: mks[i].latitude,
          longitude: mks[i].longitude,
        }})
        poi = {
          latitude: mks[i].latitude,
          longitude: mks[i].longitude,
        }
        haspoint = 2
      }
    }
    if(haspoint == 1){
      wx.showToast({
        title: '未找到此地点，请重新输入',
        icon:'none'
      })
    }
  },
  find:function(res) { //输入框的搜索图标
    var name = this.data.poiname
    console.log(name)
  },
  formName(r){ //输入框输入检测
    var aaa = r.detail.value
    if(aaa=='显示按钮'){
      this.setData({shuru:aaa,showbtn:true})
    }
    else{
      this.setData({shuru:aaa,showbtn:false})
    }
  },
  onLoad(){
    
    console.log(mks)
    var that = this
    that.setData({ //设置markers属性和地图位置poi，将结果在地图展示
      markers: mks,
    });
    wx.request({
      url: 'https://www.juquduo.com/juquduo/locations/show_locations',
      success(res){
        console.log('获取的地点列表',res)
        var point = res.data
        var newliea = []
        var y=1
        // for (var i = point.length-1; i >-1; i--) {
          for (var i = point.length-1; i >5; i--) {
          // var id = point[i].Id
          var id = y
          y++
          var latitude = point[i].Lat
          var longitude = point[i].Lng
          var iconPath = 'https://www.juquduo.com/static/resourse/yeshou/火苗-3.png'
          var width = '45'
          var height = '35'
          var item = {id:id,latitude:latitude,longitude:longitude,iconPath:iconPath,width:width,height:height};
          newliea = newliea.concat(item);
          // mks.push(newliea)
        }
        console.log('服务器获取的地点列表',newliea)
        // that.setData({markers:newliea})
      }
    })
  },
  touxiang:function(){ //获取用户头像
    var that = this
    var openid = wx.getStorageSync('openid')
      wx.showModal({
      cancelColor: 'blue',
      title: '授权',
      content: '授权登录',
      success (res) {
        if (res.confirm) {
          that.setData({imagepath:app.globalData.userInfo.avatarUrl})
          wx.redirectTo({
            url: '../epercenter/percenter',
          })
          wx.login({
      //获取code
      success: function (res) {

        // var code = res.code; //返回code
        // console.log('开始登录')
        const APP_ID ='wxe329ad2878856a4c'
        const APP_SECRET ='5dc296468d2217287361a94f31dcca55'
          wx.request({
            url:'https://www.juquduo.com/get_user_info',
            
            data: {appId: APP_ID,secret:APP_SECRET,jsCode:res.code,grantType:'authorization_code'},
            method: 'GET',
            success: function (res){
              console.log(res.data);
              var openid = JSON.parse(res.data).openid //返回openid
              console.log('openid为' + openid);
              wx.setStorageSync('openid', openid)
              wx.request({
                url: 'https://www.juquduo.com/login',
              data:{
                openId:openid,
                avatar:app.globalData.userInfo.avatarUrl,
                nickName:app.globalData.userInfo.nickName
              },
              success(resdata){
                console.log(resdata)
              }
              })
            },
            
          })
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              console.log(res)
              app.globalData.userInfo = res.userInfo

              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (app.userInfoReadyCallback) {
                app.userInfoReadyCallback(res)
              }
            },
            fail(res){
              console.log(res)
            }
          })
        }})
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  saoyisao(){ //扫一扫按钮
    wx.scanCode({
      onlyFromCamera: false,
    })
  },
  tiaozhuan(res){ //点击地图覆盖物，获取id跳转
    console.log(mks)
    console.log(res)
    wx.setStorageSync('placemsg', mks[res.detail.markerId-1])
    console.log(mks[res.detail.markerId-1])
    var xuhao = res.detail.markerId-1
    // wx.redirectTo({
      wx.switchTab({
      url: '../aacplace/aacplace',
      // url: '../aacplace/aacplace?xuhao='+ xuhao,    //switchTab不能传参
    })
    console.log('跳转到aacplace',xuhao)
  },
  dingwei(){ //调转到指定位置
    // this.setData({
    //   poi: {
    //     latitude: 39.942845,
    //     longitude: 116.481793,
    //   }
    // })
  },
  touser(){
    wx.redirectTo({
      url: '../blogin/login',
      // url:'https://www.juquduo.com'
    })
  },
  
})